package com.vanzo.talkie2;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

import java.util.ArrayList;
import java.util.List;

import cn.qqtheme.framework.picker.WheelPicker;
import cn.qqtheme.framework.widget.WheelView;
import cn.qqtheme.framework.widget.WheelView.OnItemSelectListener;

/**
 * Created by tisen on 2018/1/15.
 */

public class TriplePicker extends WheelPicker {
    private List<String> firstData = new ArrayList<>();
    private List<String> secondData = new ArrayList<>();
    private List<String> thirdData = new ArrayList<>();
    private int selectedFirstIndex = 0;
    private int selectedSecondIndex = 0;
    private int selectedThirdIndex = 0;

    private TriplePicker.OnWheelListener onWheelListener;
    private TriplePicker.OnPickListener onPickListener;

    private CharSequence firstPrefixLabel;
    private CharSequence firstSuffixLabel;
    private CharSequence secondPrefixLabel;
    private CharSequence secondSuffixLabel;
    private CharSequence thirdPrefixLabel;
    private CharSequence thirdSuffixLabel;

    private WheelView firstView1;
    private WheelView secondView1;
    private WheelView thirdView1;


    public TriplePicker(Activity activity, List<String> firstData, List<String> secondData, List<String> thirdData) {
        super(activity);
        this.firstData = firstData;
        this.secondData = secondData;
        this.thirdData = thirdData;
    }

    public void setSelectedIndex(int firstIndex, int secondIndex, int thirdIndex) {
        if (firstIndex >= 0 && firstIndex < this.firstData.size()) {
            this.selectedFirstIndex = firstIndex;
        }
        if (firstView1 != null) {
            firstView1.setSelectedIndex(firstIndex);
        }

        if (secondIndex >= 0 && secondIndex < this.secondData.size()) {
            this.selectedSecondIndex = secondIndex;
        }
        if (secondView1 != null) {
            secondView1.setSelectedIndex(secondIndex);
        }

        if (thirdIndex >= 0 && thirdIndex < this.thirdData.size()) {
            this.selectedThirdIndex = thirdIndex;
        }
        if (thirdView1 != null) {
            thirdView1.setSelectedIndex(thirdIndex);
        }

    }

    public void setFirstLabel(CharSequence firstPrefixLabel, CharSequence firstSuffixLabel) {
        this.firstPrefixLabel = firstPrefixLabel;
        this.firstSuffixLabel = firstSuffixLabel;
    }

    public void setSecondLabel(CharSequence secondPrefixLabel, CharSequence secondSuffixLabel) {
        this.secondPrefixLabel = secondPrefixLabel;
        this.secondSuffixLabel = secondSuffixLabel;
    }

    public void setThirdLabel(CharSequence thirdPrefixLabel, CharSequence thirdSuffixLabel) {
        this.thirdPrefixLabel = thirdPrefixLabel;
        this.thirdSuffixLabel = thirdSuffixLabel;
    }

    public String getSelectedFirstItem() {
        return this.firstData.size() > this.selectedFirstIndex ? (String) this.firstData.get(this.selectedFirstIndex) : "";
    }

    public String getSelectedSecondItem() {
        return this.secondData.size() > this.selectedSecondIndex ? (String) this.secondData.get(this.selectedSecondIndex) : "";
    }

    public String getSelectedThirdItem() {
        return this.thirdData.size() > this.selectedThirdIndex ? (String) this.thirdData.get(this.selectedThirdIndex) : "";
    }

    @NonNull
    protected View makeCenterView() {
        LinearLayout layout = new LinearLayout(this.activity);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.setGravity(17);
        if (!TextUtils.isEmpty(this.firstPrefixLabel)) {
            TextView firstView = this.createLabelView();
            firstView.setLayoutParams(new LayoutParams(-2, -2));
            firstView.setText(this.firstPrefixLabel);
            layout.addView(firstView);
        }

        firstView1 = this.createWheelView();
        firstView1.setLayoutParams(new LayoutParams(0, -2, 1.0F));
        layout.addView(firstView1);

        TextView secondView;
        if (!TextUtils.isEmpty(this.firstSuffixLabel)) {
            secondView = this.createLabelView();
            secondView.setLayoutParams(new LayoutParams(-2, -2));
            secondView.setText(this.firstSuffixLabel);
            layout.addView(secondView);
        }

        if (!TextUtils.isEmpty(this.secondPrefixLabel)) {
            secondView = this.createLabelView();
            secondView.setLayoutParams(new LayoutParams(-2, -2));
            secondView.setText(this.secondPrefixLabel);
            layout.addView(secondView);
        }

        secondView1 = this.createWheelView();
        secondView1.setLayoutParams(new LayoutParams(0, -2, 1.0F));
        layout.addView(secondView1);

        //
        TextView thirdView;
        if (!TextUtils.isEmpty(this.secondSuffixLabel)) {
            thirdView = this.createLabelView();
            thirdView.setLayoutParams(new LayoutParams(-2, -2));
            thirdView.setText(this.secondSuffixLabel);
            layout.addView(thirdView);
        }

        if (!TextUtils.isEmpty(this.thirdPrefixLabel)) {
            thirdView = this.createLabelView();
            thirdView.setLayoutParams(new LayoutParams(-2, -2));
            thirdView.setText(this.thirdPrefixLabel);
            layout.addView(thirdView);
        }

        thirdView1 = this.createWheelView();
        thirdView1.setLayoutParams(new LayoutParams(0, -2, 1.0F));
        layout.addView(thirdView1);
        //

        if (!TextUtils.isEmpty(this.thirdSuffixLabel)) {
            TextView thirdSuffixLabelView = this.createLabelView();
            thirdSuffixLabelView.setLayoutParams(new LayoutParams(-2, -2));
            thirdSuffixLabelView.setText(this.thirdSuffixLabel);
            layout.addView(thirdSuffixLabelView);
        }


        firstView1.setItems(this.firstData, this.selectedFirstIndex);
        firstView1.setOnItemSelectListener(new OnItemSelectListener() {
            public void onSelected(int index) {
                TriplePicker.this.selectedFirstIndex = index;
                if (TriplePicker.this.onWheelListener != null) {
                    TriplePicker.this.onWheelListener.onFirstWheeled(TriplePicker.this.selectedFirstIndex, (String) TriplePicker.this.firstData.get(TriplePicker.this.selectedFirstIndex));
                }

            }
        });
        secondView1.setItems(this.secondData, this.selectedSecondIndex);
        secondView1.setOnItemSelectListener(new OnItemSelectListener() {
            public void onSelected(int index) {
                TriplePicker.this.selectedSecondIndex = index;
                if (TriplePicker.this.onWheelListener != null) {
                    TriplePicker.this.onWheelListener.onSecondWheeled(TriplePicker.this.selectedSecondIndex, (String) TriplePicker.this.secondData.get(TriplePicker.this.selectedSecondIndex));
                }

            }
        });
        thirdView1.setItems(this.thirdData, this.selectedThirdIndex);
        thirdView1.setOnItemSelectListener(new OnItemSelectListener() {
            public void onSelected(int index) {
                TriplePicker.this.selectedThirdIndex = index;
                if (TriplePicker.this.onWheelListener != null) {
                    TriplePicker.this.onWheelListener.onThirdWheeled(TriplePicker.this.selectedThirdIndex, (String) TriplePicker.this.thirdData.get(TriplePicker.this.selectedThirdIndex));
                }

            }
        });
        return layout;
    }

    public int getSelectedFirstIndex() {
        return selectedFirstIndex;
    }

    public int getSelectedSecondIndex() {
        return selectedSecondIndex;
    }

    public int getSelectedThirdIndex() {
        return selectedThirdIndex;
    }

    public void setSelectedFirstIndex(int selectedFirstIndex) {
        this.selectedFirstIndex = selectedFirstIndex;
        firstView1.setSelectedIndex(selectedFirstIndex);
    }

    public void setSelectedSecondIndex(int selectedSecondIndex) {
        this.selectedSecondIndex = selectedSecondIndex;
        secondView1.setSelectedIndex(selectedSecondIndex);
    }

    public void setSelectedThirdIndex(int selectedThirdIndex) {
        this.selectedThirdIndex = selectedThirdIndex;
        thirdView1.setSelectedIndex(selectedThirdIndex);
    }

    public void onSubmit() {
        if (this.onPickListener != null) {
            this.onPickListener.onPicked(this.selectedFirstIndex, this.selectedSecondIndex, this.selectedThirdIndex);
        }

    }

    public void setOnWheelListener(TriplePicker.OnWheelListener onWheelListener) {
        this.onWheelListener = onWheelListener;
    }

    public void setOnPickListener(TriplePicker.OnPickListener onPickListener) {
        this.onPickListener = onPickListener;
    }

    public interface OnPickListener {
        void onPicked(int var1, int var2, int var3);
    }

    public interface OnWheelListener {
        void onFirstWheeled(int var1, String var2);

        void onSecondWheeled(int var1, String var2);

        void onThirdWheeled(int var1, String var2);
    }

}
